package xm.core
{
	import flash.utils.Dictionary;
	
	/**
	 * <br> 클래스 설명 : 대기열 클래스</br>
	 * @author 김정환
	 */	
	public class QueuePriority
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		private var _map : Array = [];
		private var _dicFn : Dictionary = new Dictionary( true );
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function QueuePriority()
		{
			
		}
		
		
		/**
		 * @param obj : XMSprite
		 * @param validateFn : Function
		 */		
		public function addQueue(obj:XMSprite, validateFn:Function):void
		{
			var len:int = _map.length;
			for (var i:int = 0; i < len; i++) 
			{
				if( _map[ i ] === obj ) return; 
			}
			
			_map.push( obj );
			_dicFn[ obj ] = validateFn;
		}
		
		/**
		 * @param obj : XMSprite
		 * @param validateFn : Function
		 * @param index : 대기열 인덱스 셋팅
		 */		
		public function addQueueAt(obj:XMSprite, validateFn:Function, index:int):void
		{
			_map.splice( Math.min( index, _map.length ), 0, obj );
			_dicFn[ obj ] = validateFn;
		}
		
		/**
		 * 대기열 실행
		 */		
		public function executeQueue():void
		{
			var len:int = _map.length;
			for (var i:int = 0; i < len; i++) 
			{
				var obj:XMSprite = _map.shift() as XMSprite;
				_dicFn[ obj ]( obj );
				delete _dicFn[ obj ];
			}
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		
		
		
		
		
		
	}
}